package cn.edu.scau.cmi.liangguan.hibernate.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.scau.cmi.liangguan.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.liangguan.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.liangguan.hibernate.domain.Student;
import cn.edu.scau.cmi.liangguan.hibernate.domain.Teacher;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class HDBManageServlet
 */
@WebServlet("/HDBManageServlet")
public class HDBManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HDBManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/json;charset=GBK");
		

		PrintWriter out = response.getWriter();
		String[] str=request.getParameterValues("sendData");
		if (request.getParameter("op").compareTo("1")==0) {
			StudentDAO studentDAO = new StudentDAO();
			
			Student student = new Student();
			student.setName(str[2]);
			switch(str[0]) {
			case "1": //add
				if (studentDAO.save(student)) {
					List<Student> list = studentDAO.findByProperty("name", str[2]);
					if (!list.isEmpty()) {
						Student student2 = list.get(0);
						cn.edu.scau.cmi.liangguan.database.domain.Student student3 = new cn.edu.scau.cmi.liangguan.database.domain.Student(student2.getId().toString(), student2.getName());
						out.println(JSONArray.fromObject(student3));	
					}
				}else {
					error(out);
				}
			break;	
			case "2": //update
				student = studentDAO.getSession().get(Student.class, Long.parseLong(str[1]));
				System.out.println("HDBManageServlet.doPost()"+student.getId());
				student.setName(str[2]);
				if (studentDAO.save(student)) {
					out.println(JSONArray.fromObject(student));
				}else {
					error(out);
				}
			break;
			case "3": //delete
				student.setId(Long.valueOf(str[1]));				
				if (studentDAO.delete(student)) {
					out.println(JSONArray.fromObject(student));
				}else {
					error(out);
				}
			break;	
			default:break;
			}
		}else {
			
			TeacherDAO teacherDAO = new TeacherDAO();
			Teacher teacher = new Teacher();
			teacher.setName(str[2]);
			switch(str[0]) {
			case "1":
				if (teacherDAO.save(teacher)) {
					List<Teacher> list = teacherDAO.findByProperty("name", str[2]);
					out.println(JSONArray.fromObject(list.get(0)));
				}else {
					error(out);
				}
			break;	
			case "2":
				//System.out.println("HDBManageServlet.doPost()"+str[1]);
				teacher = teacherDAO.getSession().get(Teacher.class, Long.parseLong(str[1]));
				teacher.setName(str[2]);
				if (teacherDAO.save(teacher)) {
					out.println(JSONArray.fromObject(teacher));
				}else {
					error(out);
				}

			break;
			case "3":
				teacher.setId(Long.parseLong(str[1]));
				if (teacherDAO.delete(teacher)) {
					out.println(JSONArray.fromObject(teacher));
				}else {
					error(out);
				}
			break;	
			default:break;
			}
			
		}
	}

	protected void error(PrintWriter out) {
		String error = "[{'error':'true'}]";
		JSONArray jsonArray = JSONArray.fromObject(error);
		out.println(jsonArray);			
	}
}
